package site.jlopen.assets.exception;

import java.io.FileNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.alibaba.fastjson2.JSON;

import site.jlopen.entity.BaseResponse;
import site.jlopen.entity.file.FileUploadRes;

@RestControllerAdvice//(basePackages = "site.jlopen.controller")
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
	private static Logger logger = LoggerFactory.getLogger(ResponseAdvice.class);
	
	/*@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Object  handleService(Exception e) {
		return new BaseResponse<String>().error(e.getMessage());
	}*/
	
	@ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public Object  NOT_FOUND() {
		logger.error("访问不存在");
		return new BaseResponse<String>().error("访问不存在");
    }
	
	@ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object handle(Exception e) {
		logger.error("程序异常：{}", e);
		return new BaseResponse<String>().error("程序异常:" + e.getMessage());
    }
	
	@ExceptionHandler(value = FileNotFoundException.class)
    @ResponseBody
    public Object fileNotFoundHandle(Exception e) {
		logger.error("文件不存在异常：{}", e);
		return new BaseResponse<String>().error("文件不存在:" + e.getMessage());
    }
	
	@ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public Object valid(MethodArgumentNotValidException e) {
		logger.error("校验异常：{}", e);
		BindingResult result = e.getBindingResult();
		return new BaseResponse<String>().error(result.getFieldError().getDefaultMessage());
    }
	
	/*@ExceptionHandler(value = NullPointerException.class)
	@ResponseBody
	public Object nullvalid(MethodArgumentNotValidException e) {
		logger.error("空指针异常：{}", e);
		BindingResult result = e.getBindingResult();
		return new BaseResponse<String>().error(result.getFieldError().getDefaultMessage());
	}*/
	
	@Override
	public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
		// 如果不需要进行封装的，可以添加一些校验手段，比如添加标记排除的注解
		return true;
	}

	@Override
	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {
		if (body instanceof BaseResponse) {
			return body;
		}
		if (body instanceof FileUploadRes) {
			return body;
		}
		if (body instanceof String) {
			return JSON.toJSONString(new BaseResponse<Object>().success("succ", body));
        }
		return new BaseResponse<Object>().success("succ", body);
	}
}
